package m.html.form;

import m.html.element.*;
import m.html.element.attr.InputType;
import m.html.element.attr.Size;
import m.html.element.other.IElement;
import m.html.element.other.InputElement;
import m.system.util.StringUtil;

import java.util.HashMap;
import java.util.Map;

public class TableEdit{
    private Element showElement;//显示元素
    private InputElement input;//输入类型
    private Map<String,String> param;//非输入型参数
    private String description;//输入组件描述
    private TableEdit(){};
    protected IElement getItemElement(String model,String field) {
        Element div;
        if(StringUtil.isSpace(description)){
            div=Element.tag("div");
        }else{
            div=Element.tag("a-tooltip")
                    .E(Element.tag("template").A("#title").E(this.description));
        }
        div.A("class","table-edit");
        Element ele=null;
        if(null!=showElement){
            ele=showElement;
        }else if(null!=input){
            this.input.setField(model+"."+field);
            ele=this.input;
        } else {
            String type = param.get("_type");
            if ("selectImage".equals(type)) {
                String imageType = param.get("imageType"), disabled = param.get("disabled");
                ele= Element.tag("a-space")
                .E(
                    Element.tag("span").A("v-if", disabled),
                    FormHtml.getSelectButton("selectImagePage(" + model + ",'" + field + "','" + imageType + "')").A("v-else"),
                    Element.tag("img").A("v-if", model + "." + field + ".thumPath").A(":src", model + "." + field + ".thumPath").A("style", "max-height:34px;")
                        .setExtend("@click=\"viewImage(" + model + "." + field + ")\""),
                    Element.tag("span").A("v-if", disabled),
                    FormHtml.getClearButton(model + "." + field + "={}").A("v-else-if", model + "." + field + ".thumPath")
                );
            } else if ("selectIcon".equals(type)) {
                String disabled = param.get("disabled"), business = param.get("business");
                ele= Element.tag("a-space")
                .E(
                    Element.tag("span").A("v-if", disabled),
                    FormHtml.getSelectButton("selectIconPage(" + model + ",'" + field + "','','"+business+"')").A("v-else"),
                    Element.tag("img").A("v-if", model + "." + field + ".path").A(":src", model + "." + field + ".path").A("style", "max-height:34px;")
                        .setExtend("@click=\"viewIcon(" + model + "." + field + ")\""),
                    Element.tag("span").A("v-if", disabled),
                    FormHtml.getClearButton(model + "." + field + "={}").A("v-else-if", model + "." + field + ".path")
                );
            } else if ("selectJsPage".equals(type) || "selectOneModel".equals(type) || "selectManyModel".equals(type)) {
                String url = param.get("url"), width = param.get("width"), f = param.get("field"),vf = param.get("viewField"), disabled = param.get("disabled");
                if (StringUtil.isSpace(url)) url = "''";
                ele= Element.tag("a-space")
                .E(
                    Element.tag("span").A("v-if", disabled),
                    FormHtml.getSelectButton(type + "(" + model + ",'" + f + "'," + url + "," + width + ")").A("v-else"),
                    Element.tag("span").E("{{"+model+"."+f+"."+vf+"}}")
                );
            } else if ("selectOneAdmin".equals(type) || "selectManyAdmin".equals(type)){
                String org = param.get("org"), role = param.get("role"), f = param.get("field"), disabled = param.get("disabled");
                ele= Element.tag("a-space")
                .E(
                    Element.tag("span").A("v-if", disabled),
                    FormHtml.getSelectButton(type + "(" + model + ",'" + f + "'," + org + "," + role + ")").A("v-else"),
                    Element.tag("span").E("{{"+model+"."+f+".realname}}")
                );
            }
        }
        return div.E(ele);
    }
    /**
     * 输入框
     */
    public static TableEdit input(){
        TableEdit item=new TableEdit();
        item.input=new Input("", InputType.TEXT);
        return item;
    }
    /**
     * 文本域
     */
    public static TableEdit textarea() {
        TableEdit item=new TableEdit();
        item.input=new Input("", InputType.TEXTAREA);
        return item;
    }
    /**
     * 整型框
     */
    public static TableEdit integer(){
        return integer("");
    }
    /**
     * 整型框
     */
    public static TableEdit integer(String suffix){
        return number(suffix,"#,##0",0);
    }
    /**
     * 数字框
     */
    public static TableEdit number(){
        return number(null);
    }
    /**
     * 数字框
     */
    public static TableEdit number(String suffix){
        return number(suffix,"#,##0.00",2);
    }
    /**
     * 数字框
     */
    public static TableEdit number(String viewFormat,int precision){
        return number("",viewFormat,precision);
    }
    /**
     * 数字框
     */
    public static TableEdit number(String suffix,String viewFormat,int precision){
        TableEdit item=new TableEdit();
        item.input=new InputNumber("",suffix,viewFormat,precision);
        return item;
    }
    /**
     * 多选框
     */
    public static TableEdit checkBox(){
        TableEdit item=new TableEdit();
        item.input=new CheckBox("");
        return item;
    }
    /**
     * 开关组件
     */
    public static TableEdit switchBox(String checkedValue,String unCheckedValue){
        TableEdit item=new TableEdit();
        item.input=new Switch("",checkedValue,unCheckedValue);
        return item;
    }

    /**
     * 日期
     */
    public static TableEdit date(){
        TableEdit item=new TableEdit();
        item.input=new DatePicker("");
        return item;
    }

    /**
     * 日期时间
     */
    public static TableEdit dateTime(){
        TableEdit item=new TableEdit();
        item.input=new DatePicker("",true);
        return item;
    }

    /**
     * 选择框
     */
    public static TableEdit select(String dict){
        TableEdit item=new TableEdit();
        item.input=new Select("",dict);
        return item;
    }

    /**
     * 单项项
     */
    public static TableEdit radio(String dict){
        TableEdit item=new TableEdit();
        item.input=new Radio("",dict);
        return item;
    }
    /**
     * 多选项
     */
    public static TableEdit checkBoxGroup(String dict){
        TableEdit item=new TableEdit();
        item.input=new CheckBoxGroup("",dict);
        return item;
    }
    /**
     * 选择项组件
     */
    public static TableEdit selectItem(String dict){
        return selectItem(dict,false);
    }

    /**
     * 选择项组件
     */
    public static TableEdit selectItem(String dict,boolean multiple){
        TableEdit item=new TableEdit();
        item.input=new SelectItem("",dict,multiple);
        return item;
    }
    /**
     * 选择树组件
     */
    public static TableEdit selectTree(String dict){
        return selectTree(dict,false,false);
    }
    /**
     * 选择树组件
     */
    public static TableEdit selectTree(String dict,boolean multiple){
        return selectTree(dict,multiple,false);
    }
    /**
     * 选择树组件
     */
    public static TableEdit selectTree(String dict,boolean multiple,boolean parent){
        TableEdit item=new TableEdit();
        item.input=new SelectTree("",dict,multiple,parent);
        return item;
    }

    /**
     * 选择图片
     */
    public static TableEdit selectImage(String imageType){
        TableEdit item=new TableEdit();
        item.param=new HashMap<>();
        item.param.put("_type","selectImage");
        item.param.put("disabled","false");
        item.param.put("imageType",imageType);
        return item;
    }
    /**
     * 选择图标
     */
    public static TableEdit selectIcon(){
        return selectIcon("B");
    }
    /**
     * 选择图标  A系统 B业务
     */
    public static TableEdit selectIcon(String business){
        TableEdit item=new TableEdit();
        item.param=new HashMap<>();
        item.param.put("_type","selectIcon");
        item.param.put("disabled","false");
        item.param.put("business",business);
        return item;
    }

    public static TableEdit selectOneModel(String url,String field,String viewField){
        return selectOneModel(url,1000,field,viewField);
    }
    /**
     * 选择js页面
     * url 需用''拼接
     */
    public static TableEdit selectJsPage(String url,int width,String field,String viewField){
        TableEdit item=new TableEdit();
        item.param=new HashMap<>();
        item.param.put("_type","selectJsPage");
        item.param.put("disabled","false");
        item.param.put("url",url);
        item.param.put("width",String.valueOf(width));
        item.param.put("field",field);
        item.param.put("viewField",viewField);
        return item;
    }
    /**
     * 选择一个model
     * url 需用''拼接
     */
    public static TableEdit selectOneModel(String url,int width,String field,String viewField){
        TableEdit item=new TableEdit();
        item.param=new HashMap<>();
        item.param.put("_type","selectOneModel");
        item.param.put("disabled","false");
        item.param.put("url",url);
        item.param.put("width",String.valueOf(width));
        item.param.put("field",field);
        item.param.put("viewField",viewField);
        return item;
    }
    /**
     * 选择多个model
     * url 需用''拼接
     */
    public static TableEdit selectManyModel(String url,String field,String viewField){
        TableEdit item=selectOneModel(url,field,viewField);
        item.param.put("_type","selectManyModel");
        return item;
    }
    /**
     * 选择一个管理员
     */
    public static TableEdit selectOneAdmin(String org,String role,String field){
        TableEdit item=new TableEdit();
        item.param=new HashMap<>();
        item.param.put("_type","selectOneAdmin");
        item.param.put("disabled","false");
        item.param.put("org",org);
        item.param.put("role",role);
        item.param.put("field",field);
        return item;
    }
    /**
     * 选择多个管理员
     */
    public static TableEdit selectManyAdmin(String org,String role,String field){
        TableEdit item=selectOneAdmin(org,role,field);
        item.param.put("_type","selectManyAdmin");
        return item;
    }

    /**
     * 设置显示元素
     */
    public static TableEdit show(Element showElement){
        TableEdit item=new TableEdit();
        item.showElement=showElement;
        return item;
    }

    /**
     * 设置尺寸
     */
    public TableEdit setSize(Size size) {
        if(null==input){
            this.param.put("size",size.toString());
        }else {
            input.setSize(size);
        }
        return this;
    }

    /** 提示文字
     * 固定汉字
     */
    public TableEdit setPlaceholder(String placeholder) {
        String[] arr=StringUtil.noSpace(placeholder).split("\\|");
        if(null==input){
            this.param.put("placeholder",arr[0]);
        }else {
            if (input instanceof CheckBox) {
                input.E(new Text(arr[0]));
            } else {
                input.setPlaceholder(arr[0]);
            }
        }
        if(arr.length>1) this.setDescription(arr[1]);
        return this;
    }

    /** 是否禁用
     * 表达式
     */
    public TableEdit setDisabled(String disabled) {
        if(null==input){
            this.param.put("disabled",disabled);
        }else {
            input.setDisabled(disabled);
        }
        return this;
    }

    /**
     * 设置输入组件描述
     */
    public TableEdit setDescription(String description){
        this.description=description;
        return this;
    }
}
